using Microsoft.AspNetCore.Mvc;
using RiceShop.Models;
using RiceShop.Services;
using Microsoft.AspNetCore.Authorization;

namespace RiceShop.Controllers
{
    /// <summary>
    /// 菜单管理控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class MenuController : ControllerBase
    {
        private readonly IMenuService _menuService;

        public MenuController(IMenuService menuService)
        {
            _menuService = menuService;
        }

        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns>菜单列表</returns>
        [HttpGet("list")]
        public async Task<ActionResult<IEnumerable<Menu>>> GetMenus()
        {
            var menus = await _menuService.GetAllMenusAsync();
            return Ok(ApiResponse<IEnumerable<Menu>>.Ok(menus, "获取菜单列表成功"));
        }

        /// <summary>
        /// 获取顶级菜单
        /// </summary>
        /// <returns>菜单列表</returns>
        [HttpGet("topList")]
        public async Task<ActionResult<IEnumerable<Menu>>> GetTopMenus()
        {
            var menus = await _menuService.GetTopMenusAsync();
            return Ok(ApiResponse<IEnumerable<Menu>>.Ok(menus, "获取顶级菜单列表成功"));
        }

        /// <summary>
        /// 获取指定菜单详情
        /// </summary>
        /// <param name="id">菜单ID</param>
        /// <returns>菜单详情</returns>
        [HttpGet("detail/{id}")]
        public async Task<ActionResult<Menu>> GetMenu(long id)
        {
            var menu = await _menuService.GetMenuByIdAsync(id);
            if (menu == null)
                return NotFound(ApiResponse<Menu>.Error("菜单不存在"));

            return Ok(ApiResponse<Menu>.Ok(menu, "获取菜单详情成功"));
        }

        /// <summary>
        /// 创建新菜单
        /// </summary>
        /// <param name="menu">菜单信息</param>
        /// <returns>创建的菜单信息</returns>
        [HttpPost("create")]
        public async Task<ActionResult<Menu>> CreateMenu(Menu menu)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var createdMenu = await _menuService.CreateMenuAsync(menu);
            return CreatedAtAction(
                nameof(GetMenu),
                new { id = createdMenu.menuId },
                ApiResponse<Menu>.Ok(createdMenu, "创建菜单成功")
            );
        }

        /// <summary>
        /// 更新菜单信息
        /// </summary>
        /// <param name="id">菜单ID</param>
        /// <param name="menu">更新的菜单信息</param>
        /// <returns>更新结果</returns>
        [HttpPut("update/{id}")]
        public async Task<IActionResult> UpdateMenu(long id, Menu menu)
        {
            if (id != menu.menuId)
                return BadRequest(ApiResponse<Menu>.Error("菜单ID不匹配"));

            var updatedMenu = await _menuService.UpdateMenuAsync(id, menu);
            if (updatedMenu == null)
                return NotFound(ApiResponse<Menu>.Error("菜单不存在"));

            return Ok(ApiResponse<Menu>.Ok(updatedMenu, "更新菜单成功"));
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id">要删除的菜单ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteMenu(long id)
        {
            var result = await _menuService.DeleteMenuAsync(id);
            if (!result)
                return NotFound(ApiResponse<object>.Error("菜单不存在或含有子菜单"));

            return Ok(ApiResponse<object>.Ok(null, "删除菜单成功"));
        }

        /// <summary>
        /// 获取指定父菜单的子菜单
        /// </summary>
        /// <param name="parentId">父菜单ID</param>
        /// <returns>子菜单列表</returns>
        [HttpGet("children/{parentId}")]
        public async Task<ActionResult<IEnumerable<Menu>>> GetMenuChildren(long parentId)
        {
            var menus = await _menuService.GetMenusByParentIdAsync(parentId);
            return Ok(ApiResponse<IEnumerable<Menu>>.Ok(menus, "获取子菜单成功"));
        }
    }
}
